﻿Imports System.Windows.Forms

Public Class frmCalibrationRpt

    Public Property DefaultCalibration As String = ""
    Public Property DefaultBranch As String = ""
    Public Property DefaultStation As String = ""
    Public Property DefaultParameter As String = ""

    Private ReadOnly Property Calibration As clsCalibration
        Get
            Return CalibrationTable(cboCalibration.Text).Calibration
        End Get
    End Property

    ''' <summary>
    ''' Return currently selected branch
    ''' </summary>
    Private Function ActiveBranch() As clsBranch
        If cboBranch.SelectedIndex = -1 Then Return Nothing
        Return Calibration.dictBranches(cboBranch.Text)
    End Function

    ''' <summary>
    ''' Return currently selected station
    ''' </summary>
    Private Function ActiveStation() As clsStation
        If ActiveBranch() Is Nothing OrElse cboStation.SelectedIndex = -1 Then Return Nothing
        Return ActiveBranch.dictStations(cboStation.Text)
    End Function

    ''' <summary>
    ''' Return currently selected parameter
    ''' </summary>
    Private Function ActiveParameter() As clsParameter
        If ActiveBranch() Is Nothing OrElse ActiveStation() Is Nothing OrElse cboParameter.SelectedIndex = -1 Then Return Nothing
        Return ActiveStation.dictParameters(cboParameter.Text)
    End Function

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click, btnOK.Click
        Me.Close()
    End Sub

    Private Sub frmCalibrationRpt_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        SaveWindowPos(REGAPP, Me)
    End Sub

    Private Sub frmCalibrationRpt_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        GetWindowPos(REGAPP, Me)
        With cboCalibration
            For Each tbl As clsTable In OpenTables
                If tbl.TableType = clsTable.enumTableType.Calibration Then .Items.Add(tbl.TableName)
            Next
            If .Items.Contains(_DefaultCalibration) Then
                .Text = _DefaultCalibration
            Else
                .SelectedIndex = IIf(.Items.Count = 0, -1, 0)
            End If
        End With
    End Sub

    Private Sub cboCalibration_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles cboCalibration.SelectedIndexChanged
        With cboBranch
            .Items.Clear()
            .Items.AddRange(New Generic.List(Of String)(Calibration.dictBranches.Keys).ToArray)
            .SelectedIndex = -1
            If .Items.Contains(_DefaultBranch) Then
                .Text = _DefaultBranch
            Else
                .SelectedIndex = IIf(.Items.Count = 0, -1, 0)
            End If
        End With
    End Sub

    Private Sub cboBranch_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles cboBranch.SelectedIndexChanged
        With cboStation
            .Items.Clear()
            .Items.Add("(All Stations--Long Report)")
            .Items.AddRange(New Generic.List(Of String)(ActiveBranch.dictStations.Keys).ToArray)
            .SelectedIndex = -1
            If .Items.Contains(_DefaultStation) Then
                .Text = _DefaultStation
            Else
                .SelectedIndex = IIf(.Items.Count <= 1, -1, 1)
            End If
        End With
    End Sub

    Private Sub cboStation_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles cboStation.SelectedIndexChanged
        With cboParameter
            .Items.Clear()
            If cboStation.Text.StartsWith("(All") Then
                Dim lstParms As New Generic.List(Of String)
                For Each sta As clsStation In ActiveBranch.dictStations.Values
                    For Each parm As String In sta.dictParameters.Keys
                        If Not lstParms.Contains(parm) Then lstParms.Add(parm)
                    Next
                Next
                lstParms.Sort()
                .Items.AddRange(lstParms.ToArray)
            Else
                .Items.AddRange(New Generic.List(Of String)(ActiveStation.dictParameters.Keys).ToArray)
            End If

            .SelectedIndex = -1

            If .Items.Contains(_DefaultParameter) Then
                .Text = _DefaultParameter
            Else
                .SelectedIndex = IIf(.Items.Count = 0, -1, 0)
            End If
        End With
    End Sub

    Private Sub cboParameter_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles cboParameter.SelectedIndexChanged
        With rptHTML
            .DocumentText = Calibration.CreateReport(cboStation.Text, cboParameter.Text)
        End With
    End Sub

    Private Sub btnCopy_Click(sender As Object, e As System.EventArgs) Handles btnCopy.Click
        Dim Header As String = String.Format("Version:1.0{0}StartHTML:0000000105{0}EndHTML:{1,10:0}{0}StartFragment:0000000105{0}EndFragment:{1,10:0}{0}", vbCrLf, rptHTML.DocumentText.Length + 105)
        Clipboard.SetDataObject(New DataObject(DataFormats.Html, Header & rptHTML.DocumentText))
    End Sub

End Class
